package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.oa.domain.OaAdminAsset;
import com.ruoyi.oa.service.IOaAdminAssetService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.OaAdminUse;
import com.ruoyi.oa.domain.vo.OaAdminUseVo;
import com.ruoyi.oa.domain.bo.OaAdminUseBo;
import com.ruoyi.oa.service.IOaAdminUseService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oa.domain.vo.OaProjectVo;


/**
 * 办公用品领用申请
 *
 * @author ruoyi
 * @date 2023-08-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/adminUse")
public class OaAdminUseController extends BaseController {

    private final IOaAdminUseService iOaAdminUseService;


    private final IOaAdminAssetService iOaAdminAssetService;

    /**
     * 查询办公用品领用申请列表
     */
//    @SaCheckPermission("oa:adminUse:list")
    @GetMapping("/list")
    public TableDataInfo<OaAdminUseVo> list(OaAdminUseBo bo, PageQuery pageQuery) {
        return iOaAdminUseService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/getAllHasFinishUse")
    public R<JSONObject> getAllHasFinishUse(){
        return R.ok(iOaAdminUseService.getAllHasFinishUse());
    }

    /**
     * 导出办公用品领用申请列表
     */
//    @SaCheckPermission("oa:adminUse:export")
    @Log(title = "办公用品领用申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaAdminUseBo bo, HttpServletResponse response) {
        List<OaAdminUseVo> list = iOaAdminUseService.queryList(bo);
        ExcelUtil.exportExcel(list, "办公用品领用申请", OaAdminUseVo.class, response);
    }

    /**
     * 获取办公用品领用申请详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:adminUse:query")
    @GetMapping("/{id}")
    public R<OaAdminUseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaAdminUseService.queryById(id));
    }

    /**
     * 新增办公用品领用申请
     */
//    @SaCheckPermission("oa:adminUse:add")
    @Log(title = "办公用品领用申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaAdminUseBo> add(@Validated(AddGroup.class) @RequestBody OaAdminUseBo bo) {
        OaAdminAsset oaAdminAsset = iOaAdminAssetService.checkHasOneData(bo.getAssetType(),bo.getAssetCode(), bo.getProductName());
        if (oaAdminAsset!=null){
            int parsed = Integer.parseInt(bo.getWareNum());
            if (parsed>oaAdminAsset.getLeaveCount()){
                return R.fail("该物品剩余数目不足领取数目");
            }
        }else {
            return R.fail("该资产编码下的物品不存在");
        }
        return R.ok(iOaAdminUseService.insertByBo(bo));
    }

    /**
     * 修改办公用品领用申请
     */
//    @SaCheckPermission("oa:adminUse:edit")
    @Log(title = "办公用品领用申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaAdminUse> edit(@Validated(EditGroup.class) @RequestBody OaAdminUseBo bo) {
        OaAdminAsset oaAdminAsset = iOaAdminAssetService.checkHasOneData(bo.getAssetType(),bo.getAssetCode(), bo.getProductName());
        if (oaAdminAsset!=null){
            int parsed = Integer.parseInt(bo.getWareNum());
            if (parsed>oaAdminAsset.getLeaveCount()){
                return R.fail("该物品剩余数目不足领取数目");
            }
        }else {
            return R.fail("该资产编码下的物品不存在");
        }
        return R.ok(iOaAdminUseService.updateByBo(bo));
    }

    /**
     * 删除办公用品领用申请
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:adminUse:remove")
    @Log(title = "办公用品领用申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaAdminUseService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
